package weaver.interfaces.xinri.duss.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import com.weaver.general.Util;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.xinri.duss.utils.InvokeHelper;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-03-21
 * @Description: 合同文档流程调用汇联易合同变更接口
 * @ModifyLog:
 **/
public class WeaContractChangeAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String remark = requestInfo.getRequestManager().getRemark();
        String fullDate = DateUtil.getFullDate();
        String src = requestInfo.getRequestManager().getSrc();
        String clientIp = requestInfo.getRequestManager().getIp();
        writeLog(">>>>>WeaContractChangeAction>>>>>requestid=" + requestid);
        try {
            JSONObject paramjson = new JSONObject();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            String htmcxz = Util.null2String(rs.getString("htmcxz"));
            String htbh = Util.null2String(rs.getString("htbh"));
            String sqrgh = Util.null2String(rs.getString("sqrgh"));
            String htjebg = Util.null2String(rs.getString("htjebg"));
            String htmc = Util.null2String(rs.getString("htmc"));
            String xdfcode = Util.null2String(rs.getString("xdfcode"));
            String sqrbm = Util.null2String(rs.getString("sqrbm"));
            String htbdwbg = Util.null2String(rs.getString("htbdwbg"));
            String sqrq = Util.null2String(rs.getString("sqrq"));
            String tjrq = Util.null2String(rs.getString("tjrq"));
            String gdrq = Util.null2String(rs.getString("gdrq"));
            String gscode = Util.null2String(rs.getString("gscode"));
            String bssj = Util.null2String(rs.getString("a5"));
            String kssj = Util.null2String(rs.getString("kssj"));
            String jssj = Util.null2String(rs.getString("jssj"));
            String htqdmd = Util.null2String(rs.getString("htqdmd"));
            JSONObject contractHead = new JSONObject();
            contractHead.put("contractNumber", htmcxz);
            contractHead.put("originContractNumber", htbh);
            contractHead.put("setOfBooksCode", "DEFAULT_SOB");
            contractHead.put("applicantEmployeeId", sqrgh);
            if (Util.getDoubleValue(htjebg) > 0) {
                contractHead.put("contractCategory", "CONTRACT");
                contractHead.put("contractTypeCode", "CON001");
            } else {
                contractHead.put("contractCategory", "FRAMEWORK_AGREEMENT");
                contractHead.put("contractTypeCode", "CON002");
            }
            contractHead.put("companyCode", gscode);
            contractHead.put("contractName", htmc);
            contractHead.put("signDate", "");
            contractHead.put("partnerCategory", "VENDOR");
            contractHead.put("partnerCode", xdfcode);
            contractHead.put("amount", htjebg);
            contractHead.put("currencyCode", "CNY");
            contractHead.put("remark", InvokeHelper.removeHTMLTags(remark));
            contractHead.put("liableCustDeptNum", sqrbm);
            contractHead.put("liablePersonEmployeeId", sqrgh);
            contractHead.put("startDate", "");
            contractHead.put("endDate", "");
            contractHead.put("status", null);
            JSONArray customFormValues = new JSONArray();
            JSONObject customField1 = new JSONObject();
            customField1.put("fieldCode", "BDW");
            customField1.put("value", htbdwbg);
            customFormValues.add(customField1);
            contractHead.put("customFormValues", customFormValues);
            contractHead.put("createdDate", sqrq);
            contractHead.put("submittedDate", tjrq);
            contractHead.put("lastApprovalDate", gdrq);
            paramjson.put("contractHead", contractHead);
            JSONArray contractLines = new JSONArray();
            //变更前
            rs.executeQuery("select * from " + tablename + "_dt2 where mainid=? and zt !=2", mainid);
            while (rs.next()) {
                JSONObject contractLine = new JSONObject();
                String zt = Util.null2String(rs.getString("zt"));
                String fkjdbm = Util.null2String(rs.getString("fkjdbm"));
                String je = Util.null2String(rs.getString("je"));
                String yzfje = Util.null2String(rs.getString("yzfje"));
                String fktj = Util.null2String(rs.getString("fktj"));
                String yjfkrq = Util.null2String(rs.getString("yjfkrq"));
                contractLine.put("stageCode", fkjdbm);
                if("0".equals(zt) && Util.getDoubleValue(htjebg) > 0){
                    contractLine.put("deleteFlag", "Y");
                }
                contractLine.put("amount", je);
                contractLine.put("partnerCode", xdfcode);
                contractLine.put("dueDate", yjfkrq);
                //contractLine.put("stagePaidPaymentAmount", yzfje);
                contractLine.put("stagePaidAdvancesAmount", 0);
                contractLine.put("partnerBankAccountNo", "");
                contractLine.put("paymentConditions", fktj);
                contractLines.add(contractLine);
            }
            rs.executeQuery("select * from " + tablename + "_dt4 where mainid=?", mainid);
            while (rs.next()) {
                JSONObject contractLine = new JSONObject();
                String fkjdbm = Util.null2String(rs.getString("fkjdbm"));
                String je = Util.null2String(rs.getString("je"));
                String yzfje = Util.null2String(rs.getString("yzfje"));
                String fktj = Util.null2String(rs.getString("fktj"));
                String yjfkrq = Util.null2String(rs.getString("yjfkrq"));
                contractLine.put("stageCode", fkjdbm);
                contractLine.put("amount", je);
                contractLine.put("partnerCode", xdfcode);
                contractLine.put("dueDate", yjfkrq);
                //contractLine.put("stagePaidPaymentAmount", yzfje);
                contractLine.put("stagePaidAdvancesAmount", 0);
                contractLine.put("partnerBankAccountNo", "");
                contractLine.put("paymentConditions", fktj);
                contractLines.add(contractLine);
            }
            paramjson.put("contractLines", contractLines);
            writeLog(">>>>>WeaContractChangeAction>>>>>paramjson=" + paramjson);
            String resultStr = InvokeHelper.change(paramjson.toJSONString());
            Map<String,Object> dataMap = new HashMap<String, Object>();
            dataMap.put("jkqqcs",paramjson.toJSONString());
            dataMap.put("jkxyfh",resultStr);
            dataMap.put("khdip",clientIp);
            dataMap.put("gllc",requestid);
            dataMap.put("oacz",src);
            dataMap.put("jkdysj",fullDate);
            dataMap.put("djh", htbh);
            dataMap.put("rqsj", DateUtil.getDateTime());
            dataMap.put("jkmc", "5");
            dataMap.put( "lcid", requestid );
            InvokeHelper.insertApiLog(dataMap,"Change");
            JSONObject resultJson = JSONObject.parseObject(resultStr);
            writeLog(">>>>>WeaContractChangeAction>>>>>resultJson=" + resultJson);
            String message = Util.null2String(resultJson.getString("message"));
            if ("operation completed".equals(message)) {
                return Action.SUCCESS;
            } else {
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent("汇联易接口返回异常:" + message);
                return Action.FAILURE_AND_CONTINUE;
            }
        } catch (
                Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口异常！");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}
